/* exercice 2*/ engendre(0,[]). engendre(N,[1|L]):-N>0, P is N-1, engendre(P,L). engendre(N,[0|L]):-N>0, P is N-1, engendre(P,L). est_dense_aux([],_). est_dense_aux([0|L],M):-M>0, P is M-1, est_dense_aux(L,P). est_dense_aux([1|L],M):-S is M+1, est_dense_aux(L,S). est_dense(L):-est_dense_aux(L,0). denses(N,L):-engendre(N,L),est_dense(L). denses_bis(N,L):-aux(N,0,L). aux(0,_,[]). aux(N,K,[1|L]):-N>0, S is K+1, P is N-1, aux(P,S,L). aux(N,K,[0|L]):-N>0, K>0, S is K-1, P is N-1, aux(P,S,L). card_dense1(N,R):-setof(L,denses(N,L),H),length(H,R). /*card_dense2(N,R):-setof(L,denses_bis(N,L),H),length(H,R).*/ /* exercice 3*/ map2_somme([],[],[]):-!. map2_somme([],_,_):-write("Arguments non valides"), fail. map2_somme(_,[],_):-write("Arguments non valides"), fail. map2_somme([X|L],[Y|G],[Z|R]):-(integer(X),integer(Y)) -> (Z is X+Y, map2_somme(L,G,R)); (write("Arguments non valides"), fail). map2(_,[],[],[]):-!. map2(_,[],_,_):-write("Arguments non valides"), fail. map2(_,_,[],_):-write("Arguments non valides"), fail. map2(P,[X|L],[Y|G],[Z|H]):-(integer(X),integer(Y)) -> (Q=..[P,X,Y,Z],Q,map2(P,L,G,H)); (write("Arguments non valides"), fail). produit(X,Y,Z):- Z is X*Y. map3(_,[],[],[],[]):-!. map3(_,[],_,_,_):-write("Arguments non valides"), fail. map3(_,_,[],_,_):-write("Arguments non valides"), fail. map3(_,_,_,[],_):-write("Arguments non valides"), fail. map3(P,[X|L],[Y|G],[W|K],[Z|H]):-(integer(X),integer(Y),integer(W)) -> (Q=..[P,X,Y,W,Z],Q,map3(P,L,G,K,H)); (write("Arguments non valides"), fail). somme_3(A,B,C,D):-D is A+B+C. /* exercice 4*/ est_formule(X):-atomic(X),!. est_formule(neg(X)):-est_formule(X). est_formule(et(X,Y)):-est_formule(X),est_formule(Y). est_formule(ou(X,Y)):-est_formule(X),est_formule(Y). symboles(X,1,0,0,0):-atomic(X). symboles(neg(P),A,N,E,O):-symboles(P,A,M,E,O), N is M+1. symboles(et(P,Q),A,N,E,O):-symboles(P,Aa,Na,Ea,Oa), symboles(Q,Ab,Nb,Eb,Ob), A is Aa+Ab, N is Na+Nb, E is Ea+Eb+1,O is Oa+Ob. symboles(ou(P,Q),A,N,E,O):-symboles(P,Aa,Na,Ea,Oa), symboles(Q,Ab,Nb,Eb,Ob), A is Aa+Ab, N is Na+Nb, E is Ea+Eb,O is Oa+Ob+1. variables(X,[X]):-atomic(X),!. variables(neg(X),L):-variables(X,L). variables(et(X,Y), L):-variables(X,L1),variables(Y,L2),union(L1,L2,L). variables(ou(X,Y), L):-variables(X,L1),variables(Y,L2),union(L1,L2,L). combine([],[],[]). combine([X|L],[Y|G],[(X,Y)|H]) :- combine(L,G,H). assoc(X,[(X,Y)|_],Y):-!. assoc(X,[_|L],Y):-assoc(X,L,Y). combiner([],[]). combiner([X|L],[(X,Y)|R]):-combiner(L,R),(Y=1;Y=0). evaluer(X,A):-atomic(X), assoc(X,A,1). evaluer(neg(X),A):-not(evaluer(X,A)). evaluer(et(X,Y),A):-evaluer(X,A),evaluer(Y,A). evaluer(ou(X,Y),A):-(evaluer(X,A), !); evaluer(Y,A). /* sat(F,A):-variables(F,L), length(L,N), engendre(N,H), combine(L,H,A), evaluer(F,A,1).*/ satisfait(F,A):-variables(F,L), combiner(L,A), evaluer(F,A).